package cn.tedu.antcolony.passport.security.iml;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Slf4j
@Service
public class GuavaBloomFilterService {
    //1.定义常量
    public static final int _1W=10000;
    //2.定义我们 guava布隆过滤器，初始容量
    public static final int SIZE=100 * _1W;
    //3.定义误判率，值越小数量也就越小
    public static double fpp=0.03;

    //4.创建 guava布隆过滤器
    public static BloomFilter<Integer> bloomFilter=BloomFilter.create(Funnels.integerFunnel(),SIZE,fpp);

     public void guavaBloomFilter(){
         //1.先让guava布隆过滤器加如100w白名单数据
         for (int i = 0; i <=SIZE ; i++) {
             bloomFilter.put(i);
         }

         //2.故意取10w个不合法数据 进行测试
         ArrayList<Integer> list = new ArrayList<>(10 * _1W);

//         for (int i = SIZE+1; i <=SIZE+(10 * _1W) ; i++) {
//             if (bloomFilter.mightContain(i)){
//                 log.info("被误判了：{}",i);
//                 list.add(i);
//             }
//         }
         for (int i = 0; i <=SIZE ; i++) {
             if (bloomFilter.mightContain(i)){
                 log.info("被误判了{}",i);
                 list.add(i);
             }
         }
         log.info("误判总数量:{}",list.size());
     }

}
